package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniu.entity.dto.AdminLoginDTO;
import com.woniu.mapper.AdminMapper;
import com.woniu.pojo.Admin;
import com.woniu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Ma.Lei
* @description 针对表【t_admin】的数据库操作Service实现
* @createDate 2022-12-16 23:27:03
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired(required = false)
    private AdminMapper adminMapper;
    @Override
    public Admin getByPhone(String phone) {
        return adminMapper.getByPhone(phone);
    }

    @Override
    public Admin getByPhoneAndPassword(AdminLoginDTO adminLoginDTO) {
        QueryWrapper<Admin> adminLoginDTOQueryWrapper = new QueryWrapper<Admin>();
        adminLoginDTOQueryWrapper.eq(adminLoginDTO.getPhone() != null,"phone",adminLoginDTO.getPhone());
        adminLoginDTOQueryWrapper.eq(adminLoginDTO.getPassword() != null,"password",adminLoginDTO.getPassword());
        Admin admin = adminMapper.selectOne(adminLoginDTOQueryWrapper);
        return admin;
    }

    /**
     * 修改密码
     *
     * @param oldPwd
     * @param newPwd
     * @param phone
     * @return Boolean
     * @author yemaf
     * @time 2022/12/26 10:56:43
     */
    @Override
    public Boolean modifyPwd(String oldPwd, String newPwd, String phone) {
        // 根据用户名查找原密码
        // 将原密码和数据库的密码比较，通过就修改密码
        // 修改新密码
        Admin admin = adminMapper.getByPhone(phone);
        if (admin == null) {
            return false;
        }
        if (oldPwd.equals(admin.getPassword())) {
            UpdateWrapper wrapper = new UpdateWrapper<>();
            wrapper.eq("phone",phone);
            admin.setPassword(newPwd);
            int result = adminMapper.update(admin, wrapper);
            return true;
        }
        return false;
    }
}




